/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinotrack.struts;

import com.dinotrack.domain.Artista;
import com.dinotrack.domain.Cancion;
import com.dinotrack.domain.CancionSQL;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author flavio
 */
public class ListaReproduccionAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private int cantidad=0;
    private ArrayList<Integer> idCanciones=new ArrayList<Integer>();

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
            
            
        if(request.getParameter("idcancion")!=null){
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");       
	    int id = Integer.parseInt(request.getParameter("idcancion"));
            
            if(request.getSession().getAttribute("idCanciones")==null)
                idCanciones=new ArrayList<Integer>();
            idCanciones.add(id);
            request.getSession().setAttribute("idCanciones", idCanciones);
            
            CancionSQL cancionSQL= new CancionSQL();
            Cancion cancion=cancionSQL.getCancionId(id);
            Set<Artista> artistas=cancion.getArtistas();
            String artists="";

            Iterator<Artista> li=artistas.iterator();
            while(li.hasNext()){
                artists+=li.next().getNombre();
                if(li.hasNext())
                    artists+=" & ";
            }
            
	    try {
	        PrintWriter writer = response.getWriter();
	        writer.print("<xml><cantidad><![CDATA["+cantidad+"]]></cantidad>"
                        + "<album><![CDATA[<div class=\"tdAlbum\"><img src=\""+cancion.getAlbum().getImagen()+"\" class=\"imgAlbum\"/>"
                        + "<div id=\"album"+cantidad+"\" class=\"botonDelete\""
                        + " onclick=\"quitarCancion('CellListaRepr"+cantidad+"')\"/></div>]]></album>"+
                        "<cancion><![CDATA[<div class=\"tdCancion\" title=\""+cancion.getNombre()+"\">"+cancion.getNombre()+"</div>]]></cancion>"+
                        "<artista><![CDATA[<div class=\"tdArtista\" title=\""+artists+"\">"+artists+"</div>]]></artista></xml>");
	        writer.flush();
	        writer.close();
                cantidad++;
	    } catch (IOException ex) {
	        Logger.getLogger(ListaReproduccionAction.class.getName()).log(Level.SEVERE, null, ex);
	    }
        }
        
        if(request.getParameter("indice")!=null){
            ((ArrayList<Integer>)request.getSession().getAttribute("idCanciones")).remove(Integer.parseInt(request.getParameter("indice")));
        }
        
        if(request.getParameter("recargar")!=null){
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");       
            StringBuilder xml=new StringBuilder();
            for(int id:idCanciones){
            
                CancionSQL cancionSQL= new CancionSQL();
                Cancion cancion=cancionSQL.getCancionId(id);
                Set<Artista> artistas=cancion.getArtistas();
                String artists="";

                Iterator<Artista> li=artistas.iterator();
                while(li.hasNext()){
                    artists+=li.next().getNombre();
                    if(li.hasNext())
                        artists+=" & ";
                }

                xml.append("<cantidad>"+cantidad+"</cantidad>"
                            + "<album><div class=\"tdAlbum\"><img src=\""+cancion.getAlbum().getImagen()+"\" class=\"imgAlbum\"/>"
                            + "<div id=\"album"+cantidad+"\" class=\"botonDelete\""
                            + " onclick=\"quitarCancion('CellListaRepr"+cantidad+"')\"/></div></album>"+
                            "<cancion><div class=\"tdCancion\" title=\""+cancion.getNombre()+"\">"+cancion.getNombre()+"</div></cancion>"+
                            "<artista><div class=\"tdArtista\" title=\""+artists+"\">"+artists+"</div></artista>");
                
            }
            try {
                PrintWriter writer = response.getWriter();
                writer.print("<xml>"+xml.toString()+"</xml>");
                writer.flush();
                writer.close();
                cantidad++;
            } catch (IOException ex) {
                Logger.getLogger(ListaReproduccionAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
        
    }
}
